//
//  ZBBPlayHistory.m
//  沙哈拉FM
//
//  Created by dllo on 15/11/5.
//  Copyright © 2015年 S1mpl3. All rights reserved.
//

#import "ZBBPlayHistory.h"
static ZBBPlayHistory *playHistory;
@implementation ZBBPlayHistory

+(ZBBPlayHistory *)shareInstance
{
    static  dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        playHistory = [[ZBBPlayHistory alloc]init];
        playHistory.playHistoryArray = [NSMutableArray array];
    });
    return playHistory;
    
}


- (void)removeHistory:(NSString *)documentPath{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    //创建一个文件
    NSArray *array = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documents = [array lastObject];
    documentPath = [documents stringByAppendingPathComponent:documentPath];
    
    //1)检验指定路径下是否有某个文件
    BOOL isExist = [fileManager fileExistsAtPath:documentPath];
    
    NSLog(@"%@", documentPath);
    
    if (isExist) {
        NSLog(@"存在");
    }else{
        NSLog(@"不存在");
    }
    //2)删除某个文件
    BOOL isRemoved = [fileManager removeItemAtPath:documentPath error:nil];
    if (isRemoved) {
        NSLog(@"删除成功");
    }else{
        NSLog(@"删除失败");
    }
}

-(void)WriteToFile:(NSString *)path
{
    NSArray *array = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    //2.找到document文件夹
    NSString *documents = [array lastObject];
    //3.拼接要存放数组的文件路径
    NSString *documentPath = [documents stringByAppendingPathComponent:path];
    NSArray *tempArray = [NSArray arrayWithArray:playHistory.playHistoryArray];
    [tempArray writeToFile:documentPath atomically:YES];
}
-(NSMutableArray *)takeHistoryArrayFromFile:(NSString *)path
{
    ;
    NSString *documents = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject;
    NSString *documnetPath = [documents stringByAppendingPathComponent:path];
    NSArray *resultArray = [NSArray arrayWithContentsOfFile:documnetPath];
    //    NSLog(@"%@", documnetPath);
    playHistory.playHistoryArray = [NSMutableArray arrayWithArray:resultArray];
    
    return  playHistory.playHistoryArray;
}
-(void)addDictionary:(NSDictionary *)dictionary
{
    if (playHistory.playHistoryArray.count != 0) {
        if (![playHistory.playHistoryArray containsObject:dictionary]) {
          
           
                
                [playHistory.playHistoryArray insertObject:dictionary atIndex:0];
                
                
           
        }
           }
  
    
    

else
{
    [playHistory.playHistoryArray addObject:dictionary];
}
}
@end
